package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.utils.glide.GlideImgManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/12/4
 * @Describe
 */
public class SimpleGoodsInfoAdapter extends RecyclerView.Adapter<SimpleGoodsInfoAdapter.PromotionActHolder> {
    private final Context context;
    private List<GoodsInfo> list;
    private String name;
    private int count;
    ISetOnNewSecondClick onSecondItemClickListener;

    public void setOnSecondItemClickListener(ISetOnNewSecondClick onSecondItemClickListener) {
        this.onSecondItemClickListener = onSecondItemClickListener;
    }

    public SimpleGoodsInfoAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    @Override
    public PromotionActHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PromotionActHolder(LayoutInflater.from(context).inflate(R.layout.item_simple_good_info, parent,
                false));
    }

    @Override
    public void onBindViewHolder(final PromotionActHolder viewHolderSecond, final int position) {
        try {
            GoodsInfo goodsData = list.get(position);
            viewHolderSecond.tv_goods_name.setText(goodsData.getGoods_name());
            viewHolderSecond.tv_goods_num.setText(goodsData.getGoods_num());
            //分类名称是否显示
            if (position == 0) {
                viewHolderSecond.lin_goods_classify.setVisibility(View.VISIBLE);

            } else {
                viewHolderSecond.lin_goods_classify.setVisibility(View.GONE);
            }
            viewHolderSecond.tv_goods_classify.setText(name+"("+count+")");
            GlideImgManager.getLoadImg(context, goodsData.getGoods_img(), viewHolderSecond.iv_goods_img);
            viewHolderSecond.tv_repertory.setText("库存：" + goodsData.getRepertory());
            if (goodsData.isSelected()) {
                viewHolderSecond.box_check.setBackgroundResource(R.drawable.box_select_s);
            } else {
                viewHolderSecond.box_check.setBackgroundResource(R.drawable.box_select_n);
            }
            viewHolderSecond.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onSecondItemClickListener)onSecondItemClickListener.onClick(position);
                }
            });
        } catch (Exception e) {
            Trace.e("异常=" + e);
        }
    }

    public List<GoodsInfo> getList() {
        return list;
    }

    public GoodsInfo get(int position){
        return list.get(position);
    }

    /**
     * 更新列表是否被选中
     * @param position 改变的位置
     */
    public void updateRightData(int position) {
        boolean isSelect = !list.get(position).isSelected();
        list.get(position).setSelected(isSelect);
        notifyItemChanged(position);
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    public interface ISetOnNewSecondClick {
        void onClick(int position);
    }

    /**
     * 设置数据
     *
     * @param list
     */
    public void setRightListData(int count,List<GoodsInfo> list, int pageNo, String name) {
        this.name = name;
        this.count = count;
        if (pageNo == 1) {
            this.list = list;
            notifyDataSetChanged();
        } else {
            this.list.addAll(list);
            notifyDataSetChanged();
        }

    }

    @Override
    public int getItemCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    class PromotionActHolder extends RecyclerView.ViewHolder {
        TextView tv_goods_name;//商品名称
        TextView tv_goods_num;//商品条码
        TextView tv_repertory;//库存
        ImageView iv_goods_img;//商品图片
        TextView tv_goods_classify;//商品分类

        LinearLayout lin_goods_classify;//分类标题
        ImageView box_check;
        View view_line;

        public PromotionActHolder(View itemView) {
            super(itemView);
            tv_goods_name = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tv_goods_num = (TextView) itemView.findViewById(R.id.tv_goods_num);
            tv_goods_classify = (TextView) itemView.findViewById(R.id.tv_goods_classify);
            tv_repertory = (TextView) itemView.findViewById(R.id.tv_repertory);
            box_check = (ImageView) itemView.findViewById(R.id.box_check);
            iv_goods_img = (ImageView) itemView.findViewById(R.id.iv_goods_img);
            lin_goods_classify = (LinearLayout) itemView.findViewById(R.id.lin_goods_classify);
            view_line = (View) itemView.findViewById(R.id.view_line);
        }
    }
}
